package com.xianggu.designMode.chainMethod.demo03.impl;

import com.xianggu.designMode.chainMethod.demo03.Order;
import com.xianggu.designMode.chainMethod.demo03.OrderHandler;
import com.xianggu.designMode.chainMethod.demo03.OrderResult;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chengjy
 * @date 2025/3/16
 */
public class CompositeValidator extends OrderHandler {
    private List<OrderHandler> validators = new ArrayList<>();

    public CompositeValidator add(OrderHandler handler) {
        validators.add(handler);
        return this;
    }

    @Override
    protected OrderResult doHandle(Order order) {
        for (OrderHandler handler : validators) {
            OrderResult result = handler.handle(order);
            if (!result.isSuccess()) return result;
        }
        return OrderResult.success();
    }
}
